<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['app:messageDefine:add']"
        >新增
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="defineList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="消息标题" align="center" prop="messageTitle"/>
      <el-table-column label="发送对象的选取范围" align="center" prop="messageSendCondition" :formatter="formatMessageSendCondition" />
      <el-table-column label="消息分类" align="center" prop="messageCate" :formatter="formatMessageCate"/>
      <el-table-column label="跳转类型" align="center" prop="messageType" :formatter="formatMessageType"/>
      <el-table-column label="推送方式" align="center" prop="messageIsPush" :formatter="formatMessageIsPush"/>
      <el-table-column label="最后发送时间" align="center" prop="lastSendDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastSendDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合计发送数" align="center" prop="totalSendCount"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleShow(scope.row)">
            详情
          </el-button>
          <el-button
            v-if="scope.row.totalSendCount === 0"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['app:messageDefine:edit']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog title="消息详细" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" label-width="100px" size="mini">
        <el-row>
          <el-col :span="12">
            <el-form-item label="推送标题：">{{ form.messageTitle }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="消息摘要：">{{ form.messageDesc }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="发送对象：">{{ form.messageSendCondition }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="消息分类：">{{ formatMessageCate(form.messageCate) }}</el-form-item>
            <el-form-item label="推送方法：">{{ formatMessageIsPush(form.messageIsPush) }}</el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="跳转类型：">{{ formatMessageType(form.messageType) }}</el-form-item>
            <el-form-item label="消息时间：">{{ (form.messageDate) }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="消息图片：">
              <el-image v-if="form.messageImgUrl" :src="form.messageImgUrl" />
              <span v-else>无</span>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="消息详情：">{{ form.messageContent }}</el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button v-if="form.totalSendCount === 0" @click="sendMessage(form.messageId)" type="primary">发 送</el-button>
        <el-button @click="open=false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDefine, getDefine, delDefine, sendMessage } from '@/api/app/messageDefine'
import Editor from '@/components/Editor'

export default {
  name: 'Define',
  components: {
    Editor
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // APP中的消息定义表格数据
      defineList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      form: {},
      open: false,
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询APP中的消息定义列表 */
    getList() {
      this.loading = true
      listDefine(this.queryParams).then(response => {
        this.defineList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.messageId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$router.push("/app/message/create");
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const messageIds = row.messageId || this.ids
      this.$confirm('是否确认删除APP中的消息定义编号为"' + messageIds + '"的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delDefine(messageIds)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },
    handleShow(row) {
      const messageId = row.messageId

      this.form = {}
      getDefine(messageId).then(response => {
        this.form = response.data
      })

      this.open = true;
    },
    formatMessageSendCondition(row) {
      if (row.messageSendCondition === 'radioOption') {
        return '定向';
      } else {
        return '筛选';
      }
    },
    formatMessageCate(row) {
      if (row === undefined) return '';
      let text, cate
      if (typeof row == 'number') cate = row
      else cate = row.messageCate
      switch (cate) {
        case 1:
          text = '未知'
          break
        case 2:
          text = '系统消息'
          break
        case 3:
          text = '业务消息'
          break
        default:
          text = '未知'
      }

      return text;
    },
    formatMessageType(row) {
      if (row === undefined) return '';
      let text, type
      if (typeof row == 'number') type = row
      else type = row.messageType

      switch (type) {
        case 1:
          text = '未知'
          break
        case 2:
          text = 'URL跳转'
          break
        case 3:
          text = '内部页跳转'
          break
        default:
          text = '未知'
      }

      return text;
    },
    formatMessageIsPush(row) {
      if (row === undefined) return '';
      let text, type
      if (typeof row == 'string') type = row
      else type = row.messageIsPush

      switch (type) {
        case "0":
          text = '站内信'
          break
        case "1":
          text = '推送'
          break
        default:
          text = '未知'
      }

      return text;
    },
    sendMessage(messageId) {
      this.$confirm('确定发送该消息吗', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return sendMessage(messageId)
      }).then(() => {
        this.getList()
        this.msgSuccess('发送成功')
      })
    }
  }
}
</script>
